<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Find News Thread
//-----------------------------------------------------------------

$forum_find_news = $db->getTable("SELECT id, cat FROM forum_subcat ".
                                 "WHERE type = '2' OR type = '6'");

if ($forum_find_news)
{
	//-----------------------------------------------------------------
	//  Navigation Stuff
	//-----------------------------------------------------------------
	
	$news = "";
	$or = "";
	
    foreach ($forum_find_news as $news_cat)
    {
        $news .= "$or t.cat = '{$news_cat['cat']}' ".
                 "AND t.subcat = '{$news_cat['id']}'";
        $or = " OR";
    }

	$rows_per_page = $config['nav_news'];
	$numrows = $db->getInfo_countRows("forum_topics", $news, "t", "id");

	$lastpage = ceil($numrows / $rows_per_page);
	$p = (int)$p;

	if ($p < 1) $p = 1;
	elseif ($p > $lastpage) $p = $lastpage;

	if ($numrows)
        $limit = "LIMIT ".($p - 1) * $rows_per_page.", $rows_per_page";
	else
        $limit = "";

	//-----------------------------------------------------------------
	//  Get Database
	//-----------------------------------------------------------------
    
	$forum_topics = $db->getTable(
    "SELECT t.id as tid, t.name, t.cat, t.subcat, t.views, t.type, ".
           "MIN(r.id) as rid, r.topic, r.user_id, r.user_user, r.date, ".
           "r.message, ".
           "COUNT(DISTINCT r.id) AS replies, ".
           "u.id AS uid, u.avatar, u.authorization, u.username, ".
           "COUNT(t.id) AS translations ".
    "FROM forum_topics AS t ".
    "LEFT JOIN forum_replies AS r ON (r.topic = t.id) ".
    "LEFT JOIN users AS u ON (r.user_id = u.id AND r.user_user = u.username) ".
    "WHERE $news AND r.id < '10000000000' ".
    "GROUP BY t.id ".
    "ORDER BY tid DESC $limit");
    
	//-----------------------------------------------------------------
	//  Start display
	//-----------------------------------------------------------------

	if ($forum_topics)
	{
		foreach($forum_topics as $idx=>$forum_topic)
		{
			//-----------------------------------------------------------------
			//  Count for ID + trick
			//-----------------------------------------------------------------

			$idx_count = str_replace("-", "", $idx +
                         (($p - 1) * $rows_per_page + 1) - $numrows - 1);
			
			//-----------------------------------------------------------------
			//  Show some HTML
			//-----------------------------------------------------------------
            
            //Multilanguage selection (if exists more than one translation)
            if ($forum_topic['translations'] > 1)
            {
                //Selected language
                $newname = $db->getLine("SELECT name FROM forum_topics ".
                "WHERE id = '{$forum_topic['tid']}' ".
                "AND language = '$sitelanguage'");
                
                $newmessage = $db->getLine("SELECT message FROM forum_replies ".
                "WHERE id = '{$forum_topic['rid']}' ".
                "AND language = '$sitelanguage'");
                
                //User preferred language
                if (!$newname && isset($is_loged_in))
                    $newname = $db->getLine("SELECT name FROM forum_topics ".
                    "WHERE id = '{$forum_topic['tid']}' ".
                    "AND language = '(SELECT language FROM users ".
                                    "WHERE id = {$_SESSION['id']})'");
                
                if (!$newmessage && isset($is_loged_in))
                    $newmessage = $db->getLine("SELECT message ".
                    "FROM forum_replies WHERE id = '{$forum_topic['rid']}' ".
                    "AND language = '(SELECT language FROM users ".
                                    "WHERE id = {$_SESSION['id']})'");
                
                //Default language
                if (!$newname)
                    $newname = $db->getLine("SELECT name FROM forum_topics ".
                    "WHERE id = '{$forum_topic['tid']}' ".
                    "AND language = '{$config['language']}'");
                
                if (!$newmessage)
                    $newmessage = $db->getLine("SELECT message ".
                    "FROM forum_replies WHERE id = '{$forum_topic['rid']}' ".
                    "AND language = '{$config['language']}'");
                
                if ($newname)
                    $forum_topic['name'] = $newname['name'];
                if ($newmessage)
                    $forum_topic['message'] = $newmessage['message'];
            }
            
            $subcat = $db->getLine("SELECT type FROM forum_subcat ".
            "WHERE id = {$forum_topic['subcat']}");
            
            if ($subcat['type'] == "6")
            {
                $associated_event = $db->getLine("SELECT day, month, year,".
                " start, end, title, location, message FROM events ".
                "WHERE topic = '{$forum_topic['tid']}'");
                
                if ($associated_event['start'])
                    $times = date("H:i", $associated_event['start'])." - ".
                             date("H:i", $associated_event['end']);
                else
                    $times = $language['Events view desc2'];
                    
                $event_message =
                "[b][u][size=11]{$associated_event['title']}[/size][/u][/b]\n\n".
                "[b]{$language['Events add/edit desc']}[/b] ".
                "{$associated_event['day']}/{$associated_event['month']}/".
                "{$associated_event['year']}\n".
                "[b]{$language['Events add/edit desc6']}[/b] $times\n".
                "[b]{$language['Events view title4']}:[/b] ".
                "{$associated_event['location']}\n\n".
                "{$associated_event['message']}";
                
                $message = parse_message($event_message);
                $event = "[Evento] ";
            }
            else
            {
                $message = parse_message($forum_topic['message']);
                $event = "";
            }
            
            echo
            "<table class='main' cellspacing='1' cellpadding='4'>".
            "<tr class='head'><td>".
            "<table width='100%' border='0' cellspacing='0' cellpadding='0'>".
            "<tr class='head'><td align='left' width='50%'>".
            "$event{$forum_topic['name']}</td>".
            "<td align='right' width='50%'>".
            "<span class='inv'>#$idx_count</span></td></tr></table></td></tr>".
            "<tr>".
            "<td class='con1'>".
            "<table width='100%' border='0' cellspacing='0' cellpadding='0'>".
            "<tr class='empty'>".
            "<td width='10%'><table border='0' width='79' cellspacing='0' ".
            "cellpadding='0'>".
            "<tr><td height='80' class='avatar'>".
            find_avatar($forum_topic['avatar'])."</td></tr></table></td>".
            "<td width='90%' valign='middle'><span style='font-size: 12px; ".
            "font-weight: bold;'><a href='$PHP_SELF?n=modules/users&amp;".
            "a=1&amp;d={$forum_topic['user_id']}'>{$forum_topic['user_user']}".
            "</a></span>".
            "<br>".find_authorization($forum_topic['authorization']).
            "<br><br><img src='templates/$template/images/buttons/date.gif' ".
            "border='0' alt='{$language['News view desc']}'> ".
            today_yesterday($forum_topic['date'])."</td>".
            "</tr>".
            "<tr><td colspan='2' style='border-top: dashed 1px #d3d3d3'>".
            "<table cellpadding='4' cellspacing='2' width='100%' ".
            "style='text-align:justify'><tr class='empty'><td class='con6'>".
            "$message</td></tr></table>".
            "</table>".
            "</td>".
            "</tr>".
            "<tr class='head'><td><table width='100%' border='0' ".
            "cellspacing='0' cellpadding='0'><tr class='empty'>".
            "<td align='left' width='50%'><a href='$PHP_SELF?".
            "n=modules/forum&amp;a=3&amp;d={$forum_topic['cat']}&amp;".
            "o={$forum_topic['subcat']}&amp;q={$forum_topic['tid']}'>".
            ($forum_topic['replies'] - 1);
            
            if (($forum_topic['replies'] - 1) == "1")
                echo " {$language['News view desc2']}";
            else
                echo " {$language['News view desc3']}";
                
            echo "</a></td><td align='right' width='50%'>{$forum_topic['views']}";
            
            if ($forum_topic['views'] == "1")
                echo " {$language['News view desc4']}";
            else echo " {$language['News view desc5']}";
            
            echo "</td></tr></table></td></tr></table><br>";
            
			//-----------------------------------------------------------------
			//  Start from 1
			//-----------------------------------------------------------------

			$idx++;
		}
	}
	else
	{
		//-----------------------------------------------------------------
		//  Reset to 0
		//-----------------------------------------------------------------

		$idx = 0;

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td>{$language['News view desc6']}</td></tr></table><br>";
	}

	//-----------------------------------------------------------------
	//  Navigation Code
	//-----------------------------------------------------------------

	if($p == 1) $nav_prev = "<tr><td class='con1' align='right'>&nbsp;</td>";
	else
	{
		$prevpage = $p-1;
		$nav_prev = "<tr><td class='con1' align='left'><a href='$PHP_SELF?n=modules/news&amp;p=$prevpage'>&laquo; {$language['News Nav previous']}</a></td>";
	}

	$nav_nr = '[ '.paginate($lastpage, $p, $PHP_SELF.'?n=modules/news').' ]';

	if($idx == $numrows) $nav_nr = NULL;

	if($p == $lastpage) $nav_next = "<td class='con1' align='right'>&nbsp;</td></tr>";
	else
	{
		$nextcode = $numrows - $idx * $p;
		if($nextcode > $idx) $how_next = $idx;
		else $how_next = $nextcode;

		$nextpage = $p+1;

		$nav_next = "<td class='con1' align='right'><a href='$PHP_SELF?n=modules/news&amp;p=$nextpage'>{$language['News Nav next']} $how_next &raquo;</a></td></tr>";
	}

	if($idx == $numrows)
	{
		$nav_head = "class='con1'";
		$nav = NULL;
	}
	else
	{
		$nav_head = "class='head'";
		$nav = ''.$nav_prev.''.$nav_next.'';
	}

	//-----------------------------------------------------------------
	//  Navigation Display
	//-----------------------------------------------------------------

	?>
		<table class="main" cellspacing="1" cellpadding="4" width="100%">
		<col width="50%"><col width="50%">
		<tr <?= $nav_head ?>>
		<td colspan="2">
		<table border="0" cellspacing="0" cellpadding="0" width="100%">
		<col width="50%"><col width="50%">
		<tr class="empty">
		<td align="left"><?= $language['News Nav nav'] ?> <b><?= $idx ?></b> <?= $language['News Nav nav2'] ?> <b><?= $numrows ?></b></td>
		<td align="right"><?= $nav_nr ?></td>
		</tr>
		</table>
		</td>
		</tr>
		<?= $nav ?>
		</table>
	<?
}
else
    echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td>{$language['News view desc7']}</td></tr></table>"
?>
